<template>
  <!--查看课题信息-->
  <div class="view-subject">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />
    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.grade" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.affiliationCollege')">
              <el-select v-model="queryForm.collegeId" @change="handleCollegeChange" filterable class="search-item" clearable>
                <el-option v-for="item in source.collegeOptions" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.optMajor')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectType')">
              <el-select v-model="queryForm.projectType" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.declarer')">
              <el-input v-model="queryForm.applicantId" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectName')">
              <el-input v-model="queryForm.projectName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectSource')">
              <el-select v-model="queryForm.projectSourceId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectSourceOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectNature')">
              <el-select v-model="queryForm.projectNatureId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectNatureOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.viewSubjectInfo')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
          </div>
        </div>
        <edu-table ref="viewSubjectTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="80"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.declarer')" min-width="120">
            <template slot-scope="scope">
              <span>{{`${scope.row.applicantName}(${scope.row.applicantId})`}}</span>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.affiliationCollege')" prop="collegeIdI18n" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectMajor')" prop="subjectIdI18n" min-width="110"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectSource')" prop="projectSource" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectNature')" prop="projectNature" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.socialPractice')" prop="practiceSocial" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'DETAIL'">
      <TopicInfo :actionType="actionType" ref="topicInfo" :basicInfoForm="subjectTopicForm"></TopicInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/viewSubjectInfo";
import mixin from 'common/src/mixins/index';
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'viewSubjectInfo',
  mixins: [mixin],
  data() {
    return {
      getList: '/api/designservice/optInfo/list',
      api: API,
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
      },
      source: {
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        subjectTypeOptions: [{ // 课题类别
          label: "毕业论文",
          value: "毕业论文",
        }, {
          label: "毕业设计",
          value: "毕业设计",
        }],
        subjectSourceOptions: [],
        subjectNatureOptions: []
      },
      indexKey: 'MAIN',
      breadPath: {
        label: this.$t('graduationDesign.viewSubjectInfo'),
        key: "MAIN",
        children: [
          {
            key: "DETAIL",
            label: this.$t('graduationDesign.details')
          }
        ]
      },
      loading: false,
      userInfo: {}, // 当前用户信息
      viewSelectedData: [],
      actionType: '',
      subjectTopicForm: {}
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + '';
    this.searchList();
    this.getCollege();
    this.getSubjectSource();
    this.getSubjectNature();
    this.userInfo = { ...this.global };
  },
  methods: {
    searchList() {
      this.$refs.viewSubjectTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {};
      this.source.subjectOptions = [];
      this.queryForm.grade = new Date().getFullYear() + '';
      this.getSubjectSource();
      this.getSubjectNature();
      this.searchList();
    },
    handleSelectChange(val) {
      this.viewSelectedData = val;
    },
    // 选择年级change
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
      this.getSubjectSource();
      this.getSubjectNature();
      this.searchList();
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 选择年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查课题来源
    getSubjectSource() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getSource(params).then(res => {
        this.source.subjectSourceOptions = this.optionFormat(res.data, "subjectSource", "id");
      }).catch();
    },
    // 查课题性质
    getSubjectNature() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getNature(params).then(res => {
        this.source.subjectNatureOptions = this.optionFormat(res.data, "subjectProperties", "id");
      }).catch();
    },
    // 下拉数据 格式化
    optionFormat(optionList = [], label = "", value = "") {
      let dataset = [];
      for (let i = 0; i < optionList.length; i++) {
        const optionObj = {
          label: optionList[i][label],
          value: optionList[i][value]
        };
        dataset.push(optionObj);
      }
      return dataset;
    },
    // 查看
    onView(row) {
      this.indexKey = "DETAIL";
      this.actionType = "VIEW_STU_INFO";
      if (row.infoType === "teaInfo") {
        this.actionType = "VIEW_TUTOR_INFO";
      }
      this.api.getDetail(row).then(res => {
        this.subjectTopicForm = { ...res.data };
        this.subjectTopicForm.grade = res.data.grade ? res.data.grade + '' : '';
      }).catch();
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { exprotList: this.viewSelectedData });
      this.api.export(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "查看课题信息.xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    TopicInfo: () => import('../components/topicInfo')
  }
}
</script>
<style lang="scss" scoped>
.view-subject {
    .search-item {
        width: 100%;
    }
}
</style>
